// ProfileService.cs
// Facebook/Framework/Service
// Copyright (c) 2007, Nikhil Kothari. All Rights Reserved.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Facebook.Service.Core;

namespace Facebook.Service {

    /// <summary>
    /// Represents the service APIs related to the user's profile page on Facebook.
    /// </summary>
    public sealed class ProfileService {

        private FacebookSession _session;

        internal ProfileService(FacebookSession session) {
            Debug.Assert(session != null);
            _session = session;
        }

        /// <summary>
        /// Gets the FBML that shows up on the profile for the current
        /// application for the specified user. This corresponds to the
        /// profile.getFBML Facebook method.
        /// </summary>
		/// <remarks>
		/// This API method only requires a session key if it is running on a desktop
		/// application.
		/// </remarks>
        /// <param name="userID">The user whose profile is to be accessed; null if it is the current user.</param>
        /// <returns>The FBML associated with the application.</returns>
        public string GetFbml(string userID)
		{
			return GetFbml(userID, ProfileTypes.Original);
		}
		/// <summary>
		/// Gets the FBML that shows up on the profile for the current
		/// application for the specified user. This corresponds to the
		/// profile.getFBML Facebook method.
		/// </summary>
		/// <remarks>
		/// This API method only requires a session key if it is running on a desktop
		/// application.
		/// </remarks>
		/// <param name="userID">The user whose profile is to be accessed; null if it is the current user.</param>
		/// <param name="profileType">The type of profile box to retrieve.</param>
		/// <returns>The FBML associated with the application.</returns>
		public string GetFbml(string userID, ProfileTypes profileType) {
            if (String.IsNullOrEmpty(userID)) {
                // Always set userID, because in the case of a Facebook page session,
                // not passing the userID has the unexpected result of Facebook
                // trying to use the UserID of the user browsing the page instead,
                // and not the page ID.

                userID = _session.UserID;
            }

			// This API method requires a session key to be passed in for desktop applications only
            FacebookRequest request = new FacebookRequest(_session, (_session.ApplicationType == FacebookApplicationType.Client));
            request.Parameters["uid"] = userID;
			request.Parameters["type"] = ((int)profileType).ToString();

            FacebookResponse response = request.InvokeMethod("profile.getFBML");
            if (response.Status == FacebookResponseStatus.Succeeded) {
                if (response.Result != null) {
                    Debug.Assert(response.Result is string);
                    return (string)response.Result;
                }
            }

            return String.Empty;
        }

        /// <summary>
        /// Sets the FBML that shows up on the profile for the current
        /// application for the specified user. This corresponds to the
        /// Facebook profile.setFBML method.
        /// </summary>
		/// <remarks>
		/// This method now just calls the most-robust overload and sends nulls for everything but
		/// the <code>profileMarkup</code> parameter.
		/// </remarks>
        /// <param name="fbmlMarkup">The new FBML markup to display on the profile.</param>
        /// <param name="userID">The user whose profile is to be accessed; null if it is the current user.</param>
        /// <returns>true if successful; false otherwise.</returns>
        [Obsolete("Use SetFbml with the profile, action and mobile FBML split out.")]
        public bool SetFbml(string fbmlMarkup, string userID) {
			return SetFbml(userID, fbmlMarkup, null, null, null);
        }

        /// <summary>
        /// Sets the FBML that shows up on the profile for the current
        /// application for the specified user. This corresponds to the
        /// Facebook profile.setFBML method.
        /// </summary>
        /// <param name="userID">The user whose profile is to be accessed; null if it is the current user.</param>
        /// <param name="profileMarkup">The new FBML markup to display on the profile.</param>
        /// <param name="actionsMarkup">The new FBML markup to display actions on the user's profile.</param>
        /// <returns>true if successful; false otherwise.</returns>
        public bool SetFbml(string userID, string profileMarkup, string actionsMarkup) {
            return SetFbml(userID, profileMarkup, actionsMarkup, null);
        }

        /// <summary>
        /// Sets the FBML that shows up on the profile for the current
        /// application for the specified user. This corresponds to the
        /// Facebook profile.setFBML method.
        /// </summary>
        /// <param name="userID">The user whose profile is to be accessed; null if it is the current user.</param>
        /// <param name="profileMarkup">The new FBML markup to display on the profile.</param>
        /// <param name="actionsMarkup">The new FBML markup to display actions on the user's profile.</param>
        /// <param name="mobileMarkup">The new FBML markup to display on the profile on mobile devices.</param>
        /// <returns>true if successful; false otherwise.</returns>
		public bool SetFbml(string userID, string profileMarkup, string actionsMarkup, string mobileMarkup)
		{
			return SetFbml(userID, profileMarkup, null, actionsMarkup, mobileMarkup);
		}
		/// <summary>
		/// Sets the FBML that shows up on the profile for the current application for the
		/// specified user. This corresponds to the Facebook profile.setFBML method.
		/// </summary>
		/// <param name="userID">The user whose profile is to be accessed; null if it is the current user.</param>
		/// <param name="profileMarkup">The FBML intended for the application profile box on the user's profile.</param>
		/// <param name="mainProfileMarkup">The FBML intended for the narrow profile box on the Wall and Feed tabs of the user's profile.</param>
		/// <param name="actionsMarkup">The FBML intended for the user's profile actions. A profile action is the link under the user's profile picture that allows a user to take an action with your application.</param>
		/// <param name="mobileMarkup">The FBML intended for mobile devices.</param>
		/// <returns>true if successful; false otherwise.</returns>
        public bool SetFbml(string userID, string profileMarkup, string mainProfileMarkup, string actionsMarkup, string mobileMarkup) {
            if (String.IsNullOrEmpty(userID)) {
                // Always set userID, because in the case of a Facebook page session,
                // not passing the userID has the unexpected result of Facebook
                // trying to use the UserID of the user browsing the page instead,
                // and not the page ID.

                userID = _session.UserID;
            }

			// Session key is only required for desktop applications
            FacebookRequest request = new FacebookRequest(_session, (_session.ApplicationType == FacebookApplicationType.Client));
            request.Parameters["uid"] = userID;
            if (String.IsNullOrEmpty(profileMarkup) == false)
			{
                request.Parameters["profile"] = profileMarkup;
            }
			if (String.IsNullOrEmpty(mainProfileMarkup) == false)
			{
				request.Parameters["profile_main"] = mainProfileMarkup;
			}
            if (String.IsNullOrEmpty(actionsMarkup) == false)
			{
                request.Parameters["profile_action"] = actionsMarkup;
            }
            if (String.IsNullOrEmpty(mobileMarkup) == false)
			{
                request.Parameters["mobile_profile"] = mobileMarkup;
            }

            FacebookResponse response = request.InvokeMethod("profile.setFBML");
            if (response.Status == FacebookResponseStatus.Succeeded)
			{
                return FacebookResponse.CoerceBoolean(response.Result);
            }

            return false;
        }
    }
}